package edu.spec.pms.controller;

import edu.spec.pms.model.dto.MvcReturn;
import org.madgecko.basic.model.Pager;
import org.madgecko.basic.util.JsonUtil;
import org.madgecko.basic.util.SecurityUtil;
import edu.spec.pms.model.dto.ExtGridReturn;
import edu.spec.pms.model.exception.PMSException;
import edu.spec.pms.model.sys.User;
import edu.spec.pms.service.iface.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

@Controller
@RequestMapping("/admin/user")
public class UserController {

	@Inject
	private IUserService userService;

	@ResponseBody
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public Object add(User u, HttpSession session) throws NoSuchAlgorithmException {
		if(!checkUserName(u)){
			return new MvcReturn(false, null, "用户名已存在");
		}
		u.setCreateDate(new Date());
		User owner = (User)session.getAttribute("user");
		u.setOwner(userService.load(owner.getId()));
		u.setRealName(owner.getRealName());
		u.setPassword(SecurityUtil.md5("aaa"));
		userService.add(u);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/delete/{id}")
	public Object delete(@PathVariable Integer id){
		userService.delete(id);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/update")
	public Object update(User u){
		User dbu = userService.load(u.getId());
		if(!dbu.getUsername().equals(u.getUsername())){
			if(!checkUserName(u)){
				return new MvcReturn(false, null, "用户名已存在");
			}
		}
		dbu.setStatus(u.getStatus());
		dbu.setDes(u.getDes());
		dbu.setUpdateDate(new Date());
		dbu.setRealName(u.getRealName());
		dbu.setUsername(u.getUsername());
		userService.update(dbu);
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/toggleStatus/{id}")
	public Object toggleStatus(@PathVariable Integer id){
		User dbu = userService.load(id);
		if(null==dbu){
			throw new PMSException("用户不存在.");
		}
		if(0==dbu.getStatus()){
			dbu.setStatus(1);
		}else if(1==dbu.getStatus()){
			dbu.setStatus(0);
		}
		userService.update(dbu);
		return new MvcReturn(true);
	}

	@RequestMapping(value="/importIn", method= RequestMethod.POST)
	public void importIn(MultipartFile file, HttpServletRequest req, HttpServletResponse resp, HttpSession session){
		try {
			boolean b = userService.importIn(file, req, session);
			MvcReturn er = new MvcReturn(b);
			resp.setContentType("text/html;charset=UTF-8");
			resp.getWriter().write(JsonUtil.getInstance().obj2json(er));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@ResponseBody
	@RequestMapping("/findUserByCon")
	public Object findUserByCon(Integer status, String con){
		Pager<User> p = userService.findUserByCon(status, con);
		return new ExtGridReturn(p.getTotal(), p.getDatas());
	}

	@ResponseBody
	@RequestMapping("/resetPwd/{id}")
	public Object resetPwd(@PathVariable Integer id){
		try {
			User u = userService.load(id);
			u.setPassword(SecurityUtil.md5("aaa"));
			userService.update(u);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return new MvcReturn(false);
		}
		return new MvcReturn(true);
	}

	@ResponseBody
	@RequestMapping("/updatePwd")
	public Object updatePwd(String oldPwd, String newPwd, HttpSession session){
		try {
			User u = (User)session.getAttribute("user");
			userService.load(u.getId());
			oldPwd = SecurityUtil.md5(oldPwd);
			newPwd = SecurityUtil.md5(newPwd);
			if(!u.getPassword().equals(oldPwd)){
				return new MvcReturn(false, null, "原始密码错误");
			}
			u.setPassword(newPwd);
			userService.update(u);
			return new MvcReturn(true);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return new MvcReturn(false);
		}
	}

	private boolean checkUserName(User user){
		String username = user.getUsername();
		User checkUser = userService.loadByName(username);
		if(checkUser==null){
			return true;
		}else{
			return false;
		}
	}

}
